package com.play.ballen.me.vm

import com.play.ballen.R
import com.play.ballen.network.ApiRepository
import com.play.ballen.network.BallenRxHelper
import com.play.ballen.utils.UserInfoManager
import com.yuven.appframework.busevent.SingleLiveEvent
import com.yuven.appframework.network.RxSchedulerHelper
import com.yuven.appframework.util.addToDisposable
import com.yuven.baselib.component.viewmodel.BaseViewModel
import com.yuven.baselib.utils.Logger
import com.yuven.baselib.utils.ResourceUtil

class SettingVM : UserInfoVM() {

    val actionLoginOutComplete = SingleLiveEvent<Void>()

    fun logout() {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        model.logout()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)

                UserInfoManager.logout()
                actionLoginOutComplete.call()
            }
            .subscribe({
                Logger.i("SettingVM.logout success")
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("SettingVM.logout error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }
}